﻿using System.Web;
using System.Web.SessionState;

namespace FolderStructureDemo.Treeview
{
   /// <summary>
   /// Returns JSON information to a jQuery tree view.
   /// </summary>
   public class AsyncTreeviewInfoHandler : IHttpHandler, IReadOnlySessionState
   {
      /// <summary>
      /// Enables processing of HTTP Web requests.
      /// </summary>
      /// <param name="context">The context for the request to process.</param>
      void IHttpHandler.ProcessRequest( HttpContext context )
      {
         // The client control is configured to use path info to indicate the folder
         // for which to request information
         // For example:
         //   http://localhost/treedata.ashx/books/technology
         // Returns information for the folder books/technology within the PDF storage folder
         // 
         //   http://localhost/treedata.ashx/
         // Returns information for the root folder
         if ( !string.IsNullOrEmpty( context.Request.PathInfo ) )
         {
            string view = TreeViewHelpers.GetJsonTreeViewData( context.Request.PathInfo );
            context.Response.ContentType = "application/json";
            context.Response.Write( view );
            context.Response.Flush();
         }
         else
         {
            // No path info; return an error
            context.Response.StatusCode = 404;
         }
         context.Response.End();
      }

      bool IHttpHandler.IsReusable
      {
         get { return true; }
      }
   }
}
